package com.zy.algorithm.v2.sort;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.zy.activity.BaseFragment;
import com.zy.algorithm.v2.R;
import com.zy.algorithm.v2.bean.QuickSimpleNode;
import com.zy.algorithm.v2.bean.SimpleNode;
import com.zy.view.LineSetView;

import java.util.List;

public abstract class BaseSortFragmentV2 extends BaseFragment {
    @Override
    protected int getLayout() {
        return R.layout.base_sort_layout_v2;
    }

    protected LineSetView lineSetView;
    TextView title;
    TextView des;
    List<? extends SimpleNode> list;
    int curIndex = 0;
    protected int endingIndex = 0;
    protected int endingSize = 0;
    protected int[] ends;

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            play(list.get(curIndex));
            curIndex += curD();
            if (curIndex < list.size()) {
                mHandler.postDelayed(runnable, 16);
            } else {
                play(list.get(list.size() - 1));
                mHandler.postDelayed(ending, 16);
                lineSetView.setQuickEnable(false);
            }
        }
    };

    Runnable ending = new Runnable() {
        @Override
        public void run() {
            getEndLineSetView().ending(ends, endingIndex);
            endingIndex = endingIndex + curD();
            if (endingIndex < endingSize) {
                mHandler.postDelayed(ending, 16);
            } else {
                getEndLineSetView().ending(ends, endingSize);
            }
        }
    };


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lineSetView = view.findViewById(R.id.base_sort_layout_v2_line_set);
        title = view.findViewById(R.id.base_sort_layout_v2_title);
        des = view.findViewById(R.id.base_sort_layout_v2_des);
        des.setText("");
        list = getDataList();

        ends = getEnds(list.get(list.size() - 1));
        list.get(list.size() - 1).getStepEnd();
        endingSize = ends.length;

        mHandler.postDelayed(runnable, 1000);

        title.setText(getTitle());
    }

    protected int[] getEnds(SimpleNode simpleNode) {
        return simpleNode.getStepEnd();
    }

    protected LineSetView getEndLineSetView() {
        return lineSetView;
    }


    protected void play(SimpleNode curNode) {
        des.setText(curNode.getDes());
        if (curNode instanceof QuickSimpleNode) {
            //快速排序设置基准点
            lineSetView.setQuickData(((QuickSimpleNode) curNode).getPivot());
        }
        lineSetView.setData(curNode.getStepEnd(), curNode.getOpIndex(), curNode.getSortedIndex());
    }

    protected abstract String getTitle();

    protected int curD() {
        return 1;
    }

    abstract List<? extends SimpleNode> getDataList();
}
